package com.zjgsu.coursecalendar.controller;


import com.zjgsu.coursecalendar.exception.NotExistException;
import com.zjgsu.coursecalendar.model.Exam;
import com.zjgsu.coursecalendar.service.ExamService;
import com.zjgsu.coursecalendar.template.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;

@RestController
public class ExamController {
    @Autowired
    private ExamService examService;

    //添加考试
    @RequestMapping("/addexam")
    public ResultBean addExam(int examId, String examName, Timestamp beginTime,Timestamp endTime,Integer userId){
        ResultBean resultBean=new ResultBean();
        Exam exam = new Exam();
        exam.setExamId(examId);
        exam.setBeginTime(beginTime);
        exam.setEndTime(endTime);
        exam.setUserId(userId);
        try {
            examService.addExams(exam);
        }catch (NotExistException e){
            resultBean.wrongParameter();
            e.printStackTrace();
        }
        return resultBean;
    }

    //删除考试
    @RequestMapping("/deleteexam")
    public ResultBean deleteExam(int examId){
        ResultBean resultBean=new ResultBean();
        try {
            examService.deleteExam(examId);
        }catch (NotExistException e){
            resultBean.wrongParameter();
            e.printStackTrace();
        }
        return resultBean;
    }

    //修改考试信息
    @RequestMapping("/updataexam")
    public ResultBean updataExam(int examId, String examName, Timestamp beginTime,Timestamp endTime,Integer userId){
        ResultBean resultBean=new ResultBean();
        Exam exam = new Exam();
        exam.setExamId(examId);
        exam.setBeginTime(beginTime);
        exam.setEndTime(endTime);
        exam.setUserId(userId);
        try {
            examService.updataExam(exam,examId);
        }catch (NotExistException e){
            resultBean.wrongParameter();
            e.printStackTrace();
        }
        return resultBean;
    }

    //查询考试信息
    @RequestMapping("/findexam")
    public ResultBean findExam(@Param("examId") Integer userid){
        ResultBean resultBean=new ResultBean();
        try {
            resultBean.setData(examService.getExams(userid));
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultBean;
    }
}
